package butines.kraz;

import java.awt.Color;
import java.awt.Graphics2D;

import butines.core.GameSystem;
import butines.core.game.Vehicle;
import butines.core.game.steering.ObstacleAvoidanceBehavior;
import butines.core.game.util.GUtils;
import butines.core.math.Vector2f;
import butines.kraz.entity.Projectile;
import butines.kraz.entity.Unit;

public class UnitDecorator {

	private Color healthBarColorHealth = new Color(0, .85f, 0, .6f);
	private Color healthBarColorDamage = new Color(.85f, 0, 0, .6f);
	private int   healthBarWidth = 32;
	
	private int   cooldownBarWidth = 32;
	private Color cooldownBar = new Color(.85f, .85f, 0, .6f);
	
	public void drawArea(Graphics2D g, float offsetX, float offsetY, Unit unit) {
		
		g.setColor(Color.white);
		g.drawString("v: " + Math.round(unit.getSpeed() * 60), 10, 60);
		g.drawString("a: " + Math.round(unit.getAccel().length() * 60), 10, 85);
		
		float x = unit.getPosition().x - offsetX;
		float y = unit.getPosition().y - offsetY;
		
		int drawX = Math.round(x-unit.getRadius());
		int drawY = Math.round(y-unit.getRadius());
		int radius = Math.round(unit.getRadius()) * 2;
		
		g.setColor(Color.yellow);
		g.drawOval(drawX, drawY, radius, radius);
		
//		radius = Math.round(unit.getRadius()) * 2;
//		drawX = Math.round(x-radius);
//		drawY = Math.round(y-radius);
//		g.drawOval(drawX, drawY, radius*2, radius*2);
		
		g.setColor(Color.red);
		radius = Math.round(unit.getSteering().getQueryRadius());
		drawX = Math.round(x-radius);
		drawY = Math.round(y-radius);
		g.drawOval(drawX, drawY, radius*2, radius*2);
	}
	
	public void drawHealthBar(Graphics2D g, float offsetX, float offsetY, Unit unit) {
		if (!unit.isDied()) {
			float x = unit.getPosition().x - offsetX - healthBarWidth / 2;
			float y = unit.getPosition().y - offsetY + 10;
			
			int drawX = Math.round(x);
			int drawY = Math.round(y);
			
			int wide = Math.round(healthBarWidth * ((float)unit.getHealth()/(float)unit.getHealthMax()));
			
			g.setColor(healthBarColorHealth);
			g.fillRect(drawX, drawY, wide, 2);
			
			if (wide < healthBarWidth) {
				g.setColor(healthBarColorDamage);
				g.fillRect(drawX + wide, drawY, healthBarWidth - wide, 2);
			}
		}
	}
	
	
	
	public void drawCooldownBar(Graphics2D g, float offsetX, float offsetY, Unit unit) {
		if (unit.getAction().isLocked()) {
			float x = unit.getPosition().x - offsetX - cooldownBarWidth / 2;
			float y = unit.getPosition().y - offsetY + 8;
			
			int drawX = Math.round(x);
			int drawY = Math.round(y);
			
			int wide = Math.round(cooldownBarWidth * unit.getAction().getTimeElapsed());
			
			g.setColor(cooldownBar);
			g.fillRect(drawX, drawY, wide, 2);
		}
	}
	
	public void drawArriveSteeringForce(Graphics2D g, float offsetX, float offsetY, Unit unit) {
		if (unit.getSteering().getArrivel().isTurned()) {
			float drawX = unit.getTarget().x - offsetX;
			float drawY = unit.getTarget().y - offsetY;
			
			g.setColor(Color.YELLOW);
			g.drawOval(Math.round(drawX), Math.round(drawY), 5, 5);
			

			Vector2f target = unit.getSteering().getArrivel().getTarget();
			
			drawX = target.x - offsetX;
			drawY = target.y - offsetY;
			g.fillOval(Math.round(drawX), Math.round(drawY), 5, 5);
			
			Vector2f steeringForce = unit.getSteering().getArrivel().getSteeringForce();
			Vector2f vec = new Vector2f(unit.getPosition());
			vec.add(steeringForce);
			
			g.drawLine(Math.round(unit.getPosition().x), Math.round(unit.getPosition().y), Math.round(vec.x), Math.round(vec.y));
			
		}
		
	}
	
	public void drawSteerAvoidance(Graphics2D g, float offsetX, float offsetY, Unit unit) {
		if (unit.getSteering().getAvoidance().isTurned()) {
			ObstacleAvoidanceBehavior avoidance = unit.getSteering().getAvoidance();
			
			Vector2f steeringForce = avoidance.getSteeringForce().copy();
			steeringForce.mult(GameSystem.getTargetFps());
			Vector2f vec = new Vector2f(unit.getPosition());
			vec.add(steeringForce);
			
			g.setColor(Color.red);
			g.drawLine(Math.round(unit.getPosition().x), Math.round(unit.getPosition().y), Math.round(vec.x), Math.round(vec.y));

			for (Vehicle v: avoidance.getObstacles()) {
				if (v == unit) {
					continue;
				}
				int drawX = Math.round(v.getPosition().x-unit.getRadius());
				int drawY = Math.round(v.getPosition().y-unit.getRadius());
				int radius = Math.round(unit.getRadius()) * 2;
				
				g.setColor(Color.red);
				g.drawOval(drawX, drawY, radius, radius);
				
			}
			
		}
		
	}
	
	public void drawAreaProjectile(Graphics2D g, float offsetX, float offsetY, Projectile projectile) {
		if (projectile.isAlive()) {
			int drawX = Math.round(projectile.getPosition().x);
			int drawY = Math.round(projectile.getPosition().y);
			int radius = Math.round(projectile.getRadius());
			g.setColor(Color.red);
			g.drawOval(drawX - radius, drawY - radius, radius*2, radius*2);
		}
	}
	
	public void drawState(Graphics2D g, float offsetX, float offsetY, Unit unit) {
		float drawX = unit.getPosition().x - offsetX;
		float drawY = unit.getPosition().y - offsetY;
		
		g.setColor(Color.white);
		GUtils.print(g, drawX, drawY-42, 1, 0, "{0} - {1}", unit.getStateMachine().getStateName(), unit.getAction().getState());
	}
	
}
